from src import image, window, event
import pygame

# 地图中格子的大小
MAP_GRID_WIDTH = 80
MAP_GRID_HEIGHT = 100
MAP_GRID_SIZE = (MAP_GRID_WIDTH, MAP_GRID_HEIGHT)


class Map:
    """
    地图类
    """
    # 地图对象
    __obj = None
    # 地图 Rect 对象
    __rect = None
    # 地图图片
    __img = None
    # 地图向右前进等待时长
    __to_right_wait_duration = 35
    # 地图向右前进已经等待的时间
    __to_right_wait_time = 0
    # 地图向右前进动画执行时长
    __to_right_duration = 150
    # 地图向右前进动画已经执行时间
    __to_right_time = 0
    # 地图每帧向右移动的距离
    __to_right_width_per_second = None
    # 地图当前所在的位置
    __position_now = None
    # 地图最终在窗口中的位置
    __position = (0, 0)
    # 地图向左移动等待时长
    __to_left_wait_duration = 20
    # 地图向左移动已经等待的时间
    __to_left_wait_time = 0
    # 地图向左移动动画执行时长
    __to_left_duration = 120
    # 地图向左移动动画已经执行时间
    __to_left_time = 0
    # 地图每帧向左移动的距离
    __to_left_width_per_second = None
    # 格子在地图中的起始位置
    __map_grid_start_position = (255, 75)
    # 地图中格子的行数
    __map_grid_row = 5
    # 地图中格子的列数
    __map_grid_col = 9
    # 地图中的格子列表
    __map_grid = []

    def __init__(self, img, to_right_wait_duration=60, to_right_duration=150, position=(-210, 0),
                 to_left_wait_duration=20, to_left_duration=120, map_grid_row=5, map_grid_col=9):
        """
        初始化地图类对象
        :param img: 地图图片
        :param to_right_wait_duration: 地图向右前进等待时长
        :param to_right_duration: 地图向右前进动画执行时长
        :param position: 地图最终在窗口中的位置
        :param to_left_wait_duration: 地图向左移动等待时长
        :param to_left_duration: 地图向左移动动画执行时长
        :param map_grid_row: 地图中格子的行数
        :param map_grid_col: 地图中格子的列数
        """
        self.__img = img
        self.__to_right_wait_duration = to_right_wait_duration
        self.__to_right_duration = to_right_duration
        # 每帧向右移动的距离
        self.__to_right_width_per_second = (window.WINDOW_WIDTH - self.__img.get_width()) / self.__to_right_duration
        self.__position = position
        self.__to_left_wait_duration = to_left_wait_duration
        self.__to_left_duration = to_left_duration
        self.__map_grid_row = map_grid_row
        self.__map_grid_col = map_grid_col
        # 创建格子对象二维列表
        for i in range(map_grid_row):
            map_grid_row_list = []
            for j in range(map_grid_col):
                map_grid = MapGrid(
                    position=(self.__map_grid_start_position[0] + j * MAP_GRID_WIDTH,
                              self.__map_grid_start_position[1] + i * MAP_GRID_HEIGHT)
                )
                map_grid_row_list.append(map_grid)
            self.__map_grid.append(map_grid_row_list)

    def draw(self):
        # 如果还在向右移动的等待时间内或者还在向右移动
        if self.__to_right_time < self.__to_right_duration:
            self.__to_right()
        # 如果还在向左移动的等待时间内或者还在向左移动
        elif self.__to_left_time < self.__to_left_duration:
            self.__to_left()
        else:
            self.__draw_map_img(self.__position, self.__draw_grid_to_map)

    def __to_right(self):
        """
        地图向右移动动画实现
        :return:
        """
        self.__position_now = (self.__to_right_width_per_second * self.__to_right_time, 0)
        self.__draw_map_img(self.__position_now)
        # 如果还处于等待时间内
        if self.__to_right_wait_time < self.__to_right_wait_duration:
            self.__to_right_wait_time += 1
        # 如果还未移动到右边界
        elif self.__to_right_time < self.__to_right_duration:
            self.__to_right_time += 1
            self.__to_left_width_per_second = (self.__position[0] - self.__position_now[0]) / self.__to_left_duration

    def __to_left(self):
        """
        地图向左移动动画实现
        :return:
        """
        self.__draw_map_img(self.__position_now)
        # 如果还在向左移动的等待时间内
        if self.__to_left_wait_time < self.__to_left_wait_duration:
            self.__to_left_wait_time += 1
            return
        # 向左移动
        self.__position_now = (self.__position_now[0] + self.__to_left_width_per_second, 0)
        # 如果还未向左移动到指定的位置
        if self.__to_left_time < self.__to_left_duration:
            self.__to_left_time += 1

    def __draw_map_img(self, position, draw_to_win_pre_handler=None):
        """
        在窗口中绘制地图图片
        :param position: 图片在窗口中的位置
        :return:
        """
        self.__obj, self.__rect = image.draw.draw_img_with_pre_handle(
            self.__img,
            position,
            (self.__img.get_width(), self.__img.get_height()),
            img_draw_to_win_pre_handler=draw_to_win_pre_handler
        )

    def __draw_grid_to_map(self, target, target_size):
        """
        将地图格子绘制到地图中
        :param target: 地图格子要绘制到的目标
        :param target_size: 地图格子要绘制到的目标的大小
        :return:
        """
        for row in self.__map_grid:
            for col in row:
                col.draw_to(target)

    def is_loaded_to_right(self):
        """
        获取地图向右移动的加载动画是否执行完成
        :return:
        """
        return self.__to_right_time >= self.__to_right_duration


class MapGrid:
    # 格子对象
    __obj = None
    # 格子 Rect 对象
    __rect = None
    # 格子的大小
    __size = (80, 100)
    # 格子的位置
    __position = (0, 0)

    def __init__(self, size=(80, 100), position=(0, 0)):
        """
        初始化地图格子对象
        :param size: 格子的大小
        :param position: 格子的位置
        """
        self.__size = size
        self.__position = position
        self.__obj = pygame.Surface(size)

    def draw_to(self, target):
        """
        在指定的目标中绘制格子对象
        :param target: 格子要绘制到的目标
        :return:
        """
        # 将地图格子绘制到目标中
        self.__rect = target.blit(self.__obj, self.__position)
        # 为格子添加鼠标悬浮时间监听
        event.mouse.obj_dict_listen_mousemotion[self] = self.__mousemotion_default_handler

    def __mousemotion_default_handler(self, is_mousemotion, key, event):
        if not is_mousemotion:
            return

    def get_rect(self):
        return self.__rect
